package com.example.recyclerview0318;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder>{

    private List<bean> data;
    private Context context;
   //始终存在的并且创建之初就要有的通过构造方法传入
    public MyAdapter(Context context) {
        this.context = context;
    }
   //后续才会有的数据通过set方法传入
    public void setData(List<bean> data) {
        this.data = data;
    }


    //创建viewHodle对象，并且传入条目布局控件对象
    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull  ViewGroup parent, int viewType) {
        return new MyHolder(LayoutInflater.from(context).inflate(R.layout.list_item,null));
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
            holder.contactName.setText(data.get(position).getName());
            holder.contactImg.setImageResource(data.get(position).getImg());
            holder.contactTalk.setText(data.get(position).getTalk());
            holder.contactTime.setText(data.get(position).getTime());
            holder.contactNum.setText(data.get(position).getNum());

            }




    @Override
    public int getItemCount() {
        return null!=data?data.size():0;
    }

    class  MyHolder extends RecyclerView.ViewHolder{

        TextView contactName;
        ImageView contactImg;
        TextView contactTalk;
        TextView contactTime;
        TextView contactNum;

        public MyHolder(@NonNull  View itemView) {
            super(itemView);
            contactName = itemView.findViewById(R.id.list_item_name);
            contactImg = itemView.findViewById(R.id.list_item_img);
            contactTalk = itemView.findViewById(R.id.text_item_talk);
            contactTime = itemView.findViewById(R.id.text_item_time);
            contactNum = itemView.findViewById(R.id.text_num);
        }
    }
}
